//$Id: StringHelper.java,v 1.1 2008/02/13 02:40:32 administrator Exp $
package com.lenovo.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



public final class PatternUtil {
	private static HashMap<String, String> faceMap = new HashMap<String, String>();
	private static String preg = "";
	private static String domain = "";
	private static Log log = LogFactory.getLog(PatternUtil.class);
	private final static String[] replaceFalg = { "{", "}" };
	public final static String topicPattern = "(?!([#\\s]{2,}))#.+?#";
	//public final static String urlPattern = "(http).+?([\\s]|$)";
	public final static String urlPattern = "https?://[^\u4e00-\u9fa5]+"; //"(http).+?([\\s]|[^\u4e00-\u9fa5]|$)";  
	public final static String urlPattern1 = "(<<).+?(/>>)";
	private final static String personPattern = "(?!(@{2,}))@[\\u4e00-\\u9fa5A-Za-z0-9_]+?([\\s@]|\\b)";
	
	public final static String specialPattern = "(\\.|\\$|\\^|\\{|\\[|\\(|\\||\\)|\\*|\\+|\\?|\\\\|\\]|\\})";
	private PatternUtil() {
	}
	public static void main(String[] args) {
		try {
		
			String str = "【新华08要闻】“新华08中国宏观经济数据预测调查”显示,机构预测4月份居民消费价格(#CPI)#同比增幅为5.15%低于3月份创下5.4%的高点,不过这一增速仍高于一季度5.0%的水平｡工业生产者出厂价格同比上涨7.2%http://homeurl.cn:8888";

			str = toShortUrl(str);
			String tmp = getShortUrl(str);
			System.out.println(tmp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String toShortUrl(String content) throws Exception{
		Pattern pattern = Pattern.compile(urlPattern);
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			String tmp = matcher.group().trim();
			String key = UrlItem.getShorUrl(tmp);
			tmp = tmp.replaceAll("(\\$|\\W|\\\\)", "\\\\$1");
			content = content.replaceAll(tmp, "<<" + key + "/>>");
		}
//		System.out.println(content);
		return content;
	}
	public static String getShortUrl(String content) {
		Pattern pattern = Pattern.compile(urlPattern1);
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			String tmp = matcher.group().trim();
			String key = tmp.replaceAll("\\<\\<", "").replaceAll("\\/\\>\\>", "");
		//	DictType dt = DictMan.getDictType("d_mblog_para", 99);
			String shortUrl = "http://www.lenvov.com/"+ key;
			String value = "<a href=\"" + shortUrl + "\" target=\"_blank\">" + shortUrl + "</a>";
			tmp = tmp.replaceAll("(\\$|\\W|\\\\)", "\\\\$1");
			content = content.replaceAll(tmp, value);
		}
	//	System.out.println(content);
		return content;
	}

	
	public static Map GetPerson(String content) {
		Map map = new HashMap();
		List replacedList = new ArrayList();
		//content = toMessageFormat(personPattern, content, replacedList);
		for (int i = 0; i < replacedList.size(); i++) {
			String value = (String) replacedList.get(i);
			String target = value.replaceAll(" ", "").replaceAll("@", "");
			map.put(target, target);
		}
		return map;
	}
	
	
	
}
